package chapter01.part5;

import chapter01.part2.CreateStream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 其他转换流的方式
 * 
 * @author insight 
 * @since 2021/7/19
 */
public class OtherTransform {
    public static void main(String[] args) {
        Supplier<Stream<String>> words = CreateStream.getWordsStream();

        // 剔除重复元素
        System.out.println(words.get().distinct().collect(Collectors.toList()));

        // 流也支持排序
        System.out.println(words.get().distinct().sorted(Comparator.comparing(String::length).reversed()).collect(Collectors.toList()));

        // 常用于 debug 的 peek
        Integer[] array =
                Stream.iterate(1, p -> p << 1).peek(System.out::println).limit(8).toArray(Integer[]::new);
        System.out.println(Arrays.toString(array));
    }
}